package com.office.personalOffice.personalInfo.controller;

import com.office.personalOffice.personalInfo.model.LoginUser;
import com.office.personalOffice.personalInfo.model.ResponseResult;
import com.office.personalOffice.personalInfo.model.TbStaff;
import com.office.personalOffice.personalInfo.service.IPersonalInfoService;
import com.office.personalOffice.personalInfo.service.LoginServcie;
import com.office.personalOffice.personalInfo.service.impl.LoginServiceImpl;
import com.office.personalOffice.personalInfo.service.impl.PersonalInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.server.Session;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2022-10-31 19:08
 */
@Controller
public class loginController {
    //    @Autowired
//    private IPersonalInfoService service;
    @Autowired
    private IPersonalInfoService service;
    @Autowired
    private LoginServiceImpl loginServcie;

    @RequestMapping("/user/login")
    @ResponseBody
    public UserDetails get(TbStaff staff) {
        System.out.println(staff);
        UserDetails user = loginServcie.loadUserByUsername(staff.getName());
        return user;

    }
    @RequestMapping("/")
    public String login() {
        return "redirect:/login.html";

    }


//    @RequestMapping("/exist")
//    @ResponseBody
//    public String userExist(TbStaff staff){
//        String login = service.userExist(staff.getName());
//        return login;
//    }
        /**
         * 获取当前登录用户信息
         * @param authentication
         * @return
         */
        @ResponseBody
        @RequestMapping("/getUser")
        public TbStaff getLoginUser (Authentication authentication,HttpSession session){
            LoginUser principal = (LoginUser) authentication.getPrincipal();
            TbStaff user = principal.getUser();
            session.setAttribute("loginUser",user);
            System.out.println(session.getAttribute("loginUser"));
            return user;
        }

    /**
     * 退出登录，
     * @param
     */
    @RequestMapping("/logout1")
    public String loginOut(Authentication authentication,HttpSession session){
        LoginUser principal = (LoginUser) authentication.getPrincipal();
        System.out.println(principal);
        loginServcie.logout(principal);
        session.invalidate();
        return "redirect:/login.html";
    }

        /**
         * 查询所有员工
         * @param
         * @return
         */
//    @ResponseBody
//    @RequestMapping("/getList")
//    public List<TbStaff> getList(){
//
//        return service.getStaffList();
//
//    }
    }
